//
//  CollocationController.m
//  Atest
//
//  Created by Dave Cai on 14/11/19.
//  Copyright (c) 2014年 蔡 俊炜. All rights reserved.
//

#import "CollocationController.h"

@interface CollocationController ()<UICollectionViewDataSource, UICollectionViewDelegate,UIScrollViewDelegate>

@end

@implementation CollocationController
- (void)dealloc
{
    [_itemArray release];
    [super dealloc];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.itemArray = [NSMutableArray array];
        self.num = 1;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationItem setTitle:@"搭配"];
    [self.view setBackgroundColor:[UIColor grayColor]];
    [self createTopButton];
    [self createScrollView];
    [self createBanner];
    [self createLittleScrollView];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -
#pragma mark 滚动条上面的button ：“精选， 分类， 店主榜”

-(void)createTopButton
{
    self.choiceButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width/3, self.view.frame.size.height/15)];
    [_choiceButton setBackgroundColor:[UIColor blueColor]];
    [self.view addSubview:_choiceButton];
    [_choiceButton setTitle:@"精选" forState:UIControlStateNormal];
    [_choiceButton addTarget:self action:@selector(moveFirstAction:) forControlEvents:UIControlEventTouchUpInside];
    [_choiceButton release];
    
    self.categroyButton = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width/3, 0, self.view.frame.size.width/3, self.view.frame.size.height/15)];
    [_categroyButton setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:_categroyButton];
    [_categroyButton setTitle:@"分类" forState:UIControlStateNormal];
    [_categroyButton addTarget:self action:@selector(moveSecondAction:) forControlEvents:UIControlEventTouchUpInside];
    [_categroyButton release];
    
    
    self.shopKeeperButton = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width * 2/3, 0, self.view.frame.size.width/3, self.view.frame.size.height/15)];
    [_shopKeeperButton setBackgroundColor:[UIColor greenColor]];
    [self.view addSubview:_shopKeeperButton];
    [_shopKeeperButton setTitle:@"店主榜" forState:UIControlStateNormal];
    [_shopKeeperButton addTarget:self action:@selector(moveThirdAction:) forControlEvents:UIControlEventTouchUpInside];
    [_shopKeeperButton release];
    
}

#pragma mark -
#pragma mark button修改偏移量
-(void)moveFirstAction:(id)sender
{
    _scroll.contentOffset = CGPointMake(0, 0);
    _labelScroll.contentOffset = CGPointMake(-_scroll.contentOffset.x / 3, 0);
    _timer  = [NSTimer scheduledTimerWithTimeInterval:7.0f target:self selector:@selector(changAction) userInfo:nil repeats:YES];

}



-(void)moveSecondAction:(id)sender
{
    
    _scroll.contentOffset = CGPointMake(self.view.frame.size.width, 0);
    _labelScroll.contentOffset = CGPointMake(-_scroll.contentOffset.x / 3, 0);
    [_timer setFireDate:[NSDate distantFuture]];

}


-(void)moveThirdAction:(id)sender
{
    
    _scroll.contentOffset = CGPointMake(self.view.frame.size.width * 2, 0);
    _labelScroll.contentOffset = CGPointMake(-_shopKeeperButton.frame.size.width * 2, 0);
    [_timer setFireDate:[NSDate distantFuture]];
}





#pragma mark -
#pragma mark “精选， 分类， 店主榜”下面的小滚动条
-(void)createLittleScrollView
{
    self.labelScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height/15- self.view.frame.size.height/50, self.view.frame.size.width, self.view.frame.size.height/50)];
    [_labelScroll  setBackgroundColor:[UIColor blackColor]];
    [_labelScroll setContentSize:CGSizeMake(self.view.frame.size.width, 0)];
    [self.view addSubview:_labelScroll];
    
    self.littleLabel = [[UILabel alloc]initWithFrame:CGRectMake(_choiceButton.frame.size.width/4, _labelScroll.frame.size.height - 3, self.view.frame.size.width/6, 3)];
    [_littleLabel setBackgroundColor:[UIColor whiteColor]];
    [_labelScroll addSubview:_littleLabel];
    
}



#pragma mark -
#pragma mark 横向移动
-(void)createScrollView
{
    
    self.scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height/15, self.view.frame.size.width, self.view.frame.size.height - 113 - self.view.frame.size.height/15)];
    [_scroll setPagingEnabled:YES];
    [_scroll setBackgroundColor:[UIColor orangeColor]];
    [self.view addSubview:_scroll];
    _scroll.delegate = self;
    [_scroll setContentSize:CGSizeMake(self.view.frame.size.width*3, 0)];
    _timer =  [NSTimer scheduledTimerWithTimeInterval:7.0f target:self selector:@selector(changAction) userInfo:nil repeats:YES];

      [_scroll release];
}

-(void)changAction
{
    [_banner setContentOffset:CGPointMake(_banner.contentOffset.x + self.view.frame.size.width, 0)];
   

    }


-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (_scroll.contentOffset.x > 0) {
    _labelScroll.contentOffset = CGPointMake(-_scroll.contentOffset.x / 3, 0);
    //关闭定时器
    [_timer setFireDate:[NSDate distantFuture]];
    }

    
    if (_banner.contentOffset.x == 0) {
        [_banner setContentOffset:CGPointMake(_banner.frame.size.width*5, 0)];
    }
    else if(_banner.contentOffset.x == _banner.frame.size.width*6) {
        [_banner setContentOffset:CGPointMake(self.view.frame.size.width, 0)];
    }
   }


-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [_timer setFireDate:[NSDate distantFuture]];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (_scroll.contentOffset.x == 0) {
         _timer  = [NSTimer scheduledTimerWithTimeInterval:7.0f target:self selector:@selector(changAction) userInfo:nil repeats:YES];
    }
   
    
}





-(void)createBanner
{
    
     self.banner = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height /4)];
    [_banner setBackgroundColor:[UIColor grayColor]];
    //应加载到collectionview上 暂时
    [_scroll addSubview:_banner];
    [_banner setPagingEnabled:YES];
    [_banner setContentSize:CGSizeMake(self.view.frame.size.width * 7, 0)];
    [_banner setContentOffset:CGPointMake(self.view.frame.size.width, 0)];
    for (int i = 0; i < 7; i ++) {
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0 + self.view.frame.size.width * i, 0, self.view.frame.size.width, self.view.frame.size.height)];
        [_banner addSubview:image];
        _banner.delegate = self;
        [image setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",12+i]]];
    }
    [_banner release];
    
}

-(void)createCollectionView
{
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    flow.itemSize = CGSizeMake(self.view.frame.size.width /5* 2, self.view.frame.size.height / 2);
    //测试
    [flow setSectionInset:UIEdgeInsetsMake(10, 5, 0, 5)];
    self.collect = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flow];
    [_collect setBackgroundColor:[UIColor cyanColor]];
    _collect.delegate = self;
    _collect.dataSource = self;
    [_collect registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"ok"];
    [_scroll addSubview:_collect];
    [_collect release];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return [_itemArray count];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ok" forIndexPath:indexPath];
    
    return cell;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
